package com.aci.test.wordfreq;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.aci.test.wordfreq.factory.impl.SelectorFactory;
import com.aci.test.wordfreq.factory.impl.SorterFactory;
import com.aci.test.wordfreq.io.SelectionPrinter;
import com.aci.test.wordfreq.io.StreamWordsIterator;
import com.aci.test.wordfreq.sort.IWordsListSelector;
import com.aci.test.wordfreq.sort.IWordsSorter;

public class Main {

	private static Logger logger = LogManager.getLogger(Main.class);
	private static final String FILE_PATH = "src/main/resources/sampleText";

	public static void main(String[] args) {
		StreamWordsIterator streamWordsIterator = null;
		try {
			File file = new File(FILE_PATH);
			FileInputStream is = new FileInputStream(file);
			streamWordsIterator = new StreamWordsIterator(is);

			// create sorter and selector
			IWordsSorter sorter = SorterFactory.getInstance().createSorter();
			IWordsListSelector selector = SelectorFactory.getInstance().createSelector();

			// print selection
			SelectionPrinter selectionPrinter = new SelectionPrinter(sorter, selector);
			selectionPrinter.printSelected(streamWordsIterator, System.out);
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (null != streamWordsIterator) {
				try {
					streamWordsIterator.close();
				} catch (IOException e) {
					// ignore
				}
			}
		}
	}

}
